<?php


namespace app\text\controller;


use app\text\model\Profile;

class Grade
{
  public function index(){
      //hasOne
          //关联查询
      //方法1
//          $data = \app\text\model\User::get(20);
//          return json($data->profile->hobby);

      //方法2
//      $abc = \app\text\model\User::hasWhere('profile',['id'=>2])->find();
//      echo $abc->username;
      //方法3
//       $user =\app\text\model\User::hasWhere('profile',function($query){
//           $query->where('profile.id',2);
//       })->find();
//       return $user;
          //关联修改
//            $user = \app\text\model\User::get(20);
//            $user->profile->save(['hobby' =>'abc']);
          //关联新增
//      $user = \app\text\model\User::get(20);
//      $user->profile()->save(['hobby' =>'abc']);


         //belongTo
      //关联查询

//
//      $profile = Profile::get(2);
//      return $profile->user->username;


      //一对多
//      $user = \app\text\model\User::get(20);
//      //return json($user->profile);
//     return json($user->profile()->where('id','>',1)->select());

     //使用 has 方法 查询关联附表的主表内容
//      $data = \app\text\model\User::has('profile','>=',2)->select();
//     return json($data);

      //hasWhere()  查询关联附表筛选后记录  比如兴趣审核通过的主表记录
//      $data = \app\text\model\User::hasWhere('profile',['status'=>1])->select();
//      return json($data);

      //save saveall 进行关联新增  和 批量关联新增
//      $user = \app\text\model\User::get(25);
//      $user->profile()->save(['hobby'=>'看小说','status'=>1]);
//      $user->profile()->saveAll([
//          ['hobby'=>'看小说','status'=>1],
//          ['hobby'=>'玩游戏','status'=>0]
//      ]);

      //together 可以删除主表内容时，将附表关联的内容 全部删除
//      $user = \app\text\model\User::get(253,'profile');
//      $user->together('profile')->delete();
  }

  public function before(){
      //关联预载入
      //关联预载入减少了查询次数 提高了性能 但不支持多次调用

      //with（） 是 IN 方式查询
      //$list = \app\text\model\User::with('profile,book')->all([20,25,27]);
//      $list = \app\text\model\User::all([20,25,27],'profile,book');
//      foreach($list as $user){
//          dump($user->profile.$user->Book);
//      }

      //如果想要JSON查询方案，可以使用 withJoin

      //$list = \app\text\model\User::withJoin(['profile'=>['hobby']])->all([20,25,27]);
//      $list = \app\text\model\User::withJoin(['profile'=>function($query){
//         $query->withField('hobby');
//      }])->all([20,25,27]);
//            foreach($list as $user){
//          dump($user->profile);
//      }

      //关联预载入还提供了一个延迟预载入，就是先执行 all()再 load()载入；
      $list = \app\text\model\User::all([20,25,27]);
      //....
      $list->load('profile');
            foreach($list as $user){
          dump($user->profile.$user->Book);
      }
  }

  public function Count(){
      //关联统计的输出采用“关联方法名”_count 这种结构输出
      $list = \app\text\model\User::withCount('profile')->all([20,25,27]);
      foreach($list as $user){
          echo $user->profile_count.'<br>';
      }
     //withMax()、withMin()、withSum()、withAvg()等；
      //除了 withCount()不需要指定字段，其它均需要指定统计字段；
      $list = \app\text\model\User::withSum(['profile'=>'ab'],'status')->all([20,25,27]);
      foreach($list as $user){
          echo $user->ab.'<br>';
      }
  }
   public function view(){
      $list = \app\text\model\User::with('profile')->select();
      //return json($list->hidden(['username','profile.status']));
       //return json($list->hidden(['username','profile'=>['status','id']]));

       //使用visable 只显示附表相关字段
//       return json($list->visible(['profile.status']));


       //使用append()方法   添加一个额外的字段  比如另一个关联的对象属性
       return json($list->append(['book.title']));

  }

  public function many(){
//      $user = \app\text\model\User::get(20);
//      return json($user->roles);
//
//      $user = \app\text\model\User::get(20);
//      $user->roles()->save(['type'=>'测试管理员']);  saveAll

//      $user = \app\text\model\User::get(20);
//      $user->roles()->save(1);  //saveAll

//      $user = \app\text\model\User::get(20);
//      $user->roles()->attach(2,['details'=>'测试']);

      $user = \app\text\model\User::get(20);
      $user->roles()->detach([1,2]);
  }
}